package com.graplo.gwtgaefb.client.examples;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.graplo.gwtgaefb.client.JsUtil;
import com.graplo.gwtgaefb.client.ResponseHandler;

/**
 *
 * @author Dawid Ostrowski
 *
 * Universal component that generates a TextBox for each required parameter
 * moreover it contains TextArea to show FB API response
 *
 */
public class GeneralRequestPanel extends HorizontalPanel implements ResponseHandler {

	private TextArea resultTextArea;
	private Map<String, TextBox> paramTextBoxes = new HashMap<String, TextBox>();

	public GeneralRequestPanel(String name, String [] paramNames, String [] defaultValues, final CallFacebookClient callFacebookClient) {
		Button button = new Button(name);
		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.add(createParamsGrid(paramNames, defaultValues));
		verticalPanel.add(button);
		add(verticalPanel);

		resultTextArea = new TextArea();
		resultTextArea.addStyleName("response-panel");
		add(resultTextArea);

		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				callFacebookClient.call(collectParams(), GeneralRequestPanel.this);
			}
		});
		JsUtil.log("GeneralRequestPanel created");
	}

	private Map<String, String> collectParams() {
		Map<String, String> params = new HashMap<String, String>();
		for (String key : paramTextBoxes.keySet()) {
			params.put(key, paramTextBoxes.get(key).getValue());
		}
		return params;
	}

	private Widget createParamsGrid(String [] paramNames, String [] defaultValues) {
		paramTextBoxes.clear();
		if (paramNames != null) {
			int paramsCount = paramNames.length;
			if (paramsCount > 0) {
				Grid grid = new Grid(paramsCount, 2);
				for (int i=0; i<paramsCount; i++) {
					String param = paramNames[i];
					Label label = new Label(param);
					TextBox paramTextBox = new TextBox();
					paramTextBox.setText(defaultValues[i]);
					grid.setWidget(i, 0, label);
					grid.setWidget(i, 1, paramTextBox);
					paramTextBoxes.put(param, paramTextBox);
				}
				return grid;
			}
		}
		return new Label("No parameters");
	}


	@Override
	public void handle(JavaScriptObject jso) {
		if (jso != null) {
			JSONObject jsonObject = new JSONObject(jso);
			resultTextArea.setText(jsonObject.toString());
		} else {
			resultTextArea.setText("null");
		}
	}

}
